// TriangleArea.cpp: 定义控制台应用程序的入口点。
//求三角面积比例
//已知三角形的是等边三角形,三等分点

#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;

//结构体，点
struct Point
{
	double x;
	double y;
};
//线
struct  Line
{
	double a;
	double b;
	double c;
};
//类，三角形
class Triangle
{
private:
	Point a;
	Point b;
	Point c;
	double l1, l2, l3;
	double getL(Point a, Point b);
public:
	Triangle() {};
	Triangle(Point a, Point b, Point c);
	double getS();
	Point getA() { return a; }
	Point getB() { return b; }
	Point getC() { return c; }
};
//构造函数
Triangle::Triangle(Point a, Point b, Point c) {
	this->a = a;
	this->b = b;
	this->c = c;
	l1 = getL(a, b);//ab
	l2 = getL(a, c); //ac
	l3 = getL(b, c); //bc
}
//计算边长
double Triangle::getL(Point a, Point b) {
	return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
//计算三角形面积
double Triangle::getS() {
	double l = (l1 + l2+l3) / 2;
	return sqrt(l*(l - l1)*(l - l2)*(l - l3));
}

//获取直线参数
Line getLine(Point a1, Point b1) {
	double a, b,c;
	a = b1.y - a1.y;
	b = a1.x - b1.x;
	c = b1.x*a1.y - a1.x*b1.y;
	Line l = { a,b,c };
	return l;
}
//获得交点坐标（b1c2-b2c1）/(a1b2-a2b1),(a1c2-a2c1)/(b1a2-b2a1)
Point getLinePoint(Line l1, Line l2) {
	double x = (l1.b*l2.c - l2.b*l1.c) / (l1.a*l2.b - l2.a*l1.b);
	double y = (l1.a*l2.c - l2.a*l1.c) / (l1.b*l2.a - l2.b*l1.a);
	Point p = { x,y };
	return p;
}

//获得内三角形
Triangle getT2(Triangle t) {
	Point a1 = t.getA();
	Point b1 = t.getB();
	Point c1 = t.getC();

    //分别是边ab,边ac,边bc上的三等分点
	Point x = { (b1.x - a1.x) * 2 / 3 + a1.x,(b1.y - a1.y) * 2 / 3 + a1.y };
	Point y = { (c1.x - a1.x) / 3+ a1.x,(c1.y - a1.y) / 3 + a1.y };
	Point z = { (b1.x - c1.x) / 3+ c1.x,(b1.y - c1.y) / 3 + c1.y };

	Line l1 = getLine(a1, z); //直线az
	Line l2 = getLine(b1, y); //直线by
	Line l3 = getLine(c1, x); //直线cx

	Point a2 = getLinePoint(l1, l3);
	Point b2 = getLinePoint(l1, l2);
	Point c2 = getLinePoint(l2, l3);

	Triangle t2(a2, b2, c2);
	return t2;
}
int main()
{
	Point a = {0,0};
	Point b = { 1.0,0 };
	double cy =sqrt(1.0 - pow(0.5, 2));
	Point c = {0.5,cy};
	Triangle t(a,b,c);  //外等边三角形
	Triangle t2 = getT2(t);
	double s1 = t.getS();
	double s2 = t2.getS();
	double res = s1 / s2;

	cout << res << endl;

	system("pause");
    return 0;
}